<?php
namespace app\admin\controller;

use app\admin\common\Adminbase;
use app\admin\validate\Aadmimember;
use service\FeishuService;
use service\GuardService;
use service\TreeService;
use service\UtilService;
use think\Cache;
use think\Db;
use think\Exception;


class Index extends Adminbase
{

    public function index(){
        return $this->fetch();
    }

    /**
     * @name: cache_clear
     * @describe:刷新系统缓存
     */
    public function cache_clear(){
        if($this->request->isPost()) {
            $Pass = $this->request->post("name");
            if(empty($Pass)) {
                $this->error("清除失败");
            }

            $type=config("cache.type");
            if($type=="redis"){
                Cache::tag('album_data')->clear();
                Cache::tag('article_data')->clear();
                Cache::tag('navigate_data')->clear();
            }else{
                //clear_cache();//清除缓存文件
            }
            //clear_log(); //清除系统日志
            clear_temp();//清除系统模板文件
            $this->success();

        }

    }

    /**
     * @name: changepwd
     * @describe:修改密码
     */
    public function changepwd(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //dump($data);exit;
            try {
                //当前密码
                $pass = db("admin_member")->where("id",self::$admin_info['id'])->value("password");
                $curpass = UtilService::think_decrypt($pass);
                if($data['oldpassword'] != $curpass){
                    $this->error("当前密码输入错误，请重新输入！");
                }
                //dump($data);exit;
                if($data['sg_password'] != $data['repassword']){
                    $this->error("新密码两次输入不一致，请重新输入！");
                }
                if($data['oldpassword'] == $data['sg_password']){
                    $this->error("新密码和原密码一样，无需修改！");
                }
                unset($data['oldpassword']);
                unset($data['repassword']);
                $valdata = new Aadmimember();//实例化验证器
                //验证数据合法性
                if(!$valdata->check($data)) {
                    $this->error('提交失败：'.$valdata->getError());
                }
                $data['password'] = UtilService::think_encrypt($data['sg_password']);//密码加密,可逆加密
                try{
                    $this->AddLogs('修改密码');
                    db("admin_member")->where(['id'=>self::$admin_info['id']])->update(['password'=>$data['password']]);
                    $this->success("操作成功");
                }catch (Exception $exception){
                    $this->error($exception->getMessage());
                }
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }
        return $this->fetch();
    }

    public function test(){
        exit;
        set_time_limit(0);
        //$objPHPExcel = new \PHPExcel();
        $exclePath = "dep2.xlsx";
        $filename = ROOT_PATH . 'public' . DS . $exclePath;
        //dump($filename);exit;
        $extension = strtolower( pathinfo($filename, PATHINFO_EXTENSION) );
        if ($extension =='xlsx') {
            $objReader = new \PHPExcel_Reader_Excel2007();
            $objExcel = $objReader ->load($filename);
        } else if ($extension =='xls') {
            $objReader = new \PHPExcel_Reader_Excel5();
            $objExcel = $objReader->load($filename);
        }
        $excel_array=$objExcel->getsheet(0)->toArray();
        //dump($excel_array);exit;
        unset($excel_array[0]);
        unset($excel_array[1]);
        $depids = array_unique(array_column($excel_array,0));
        //dump($depids);
        foreach ($excel_array as $row){
            $depinfo = db("department")->where(['department_id'=>$row[0]])->find();
            if(empty($depinfo)){
                echo '不存在';
                dump($row);
            }
            else{
                if($depinfo['is_jtzb'] != 1){
                    echo '不在集团总部';
                    dump($row);
                }
            }
        }
        exit;
        $ctime = time();
        FeishuService::instance()->GetchildrendepByfdepid(10000000,$ctime);exit;
        $main_department_lists = FeishuService::instance()->GetDeparmentlists();
        dump($main_department_lists);exit;
        $main_department_lists = Db::name("department")
            ->where(['status'=>1])
            ->order("pid asc,sort asc")->select();
        //dump($main_department_lists);exit;
        $depids = [];
        //绿城中国控股有限公司+其他-外协单位-数字化建设中心服务团队
        //10000000 绿城中国控股有限公司
        //99990176 其他
        //99900001 外协单位
        //94001249 数字化建设中心服务团队
        $depids[] = $mainroot = 10000000;
        //$depids[] = $mainroot = 10002154;
        $treelists = TreeService::listToTree($main_department_lists,'department_id', $pid = 'pid', $child = '_child', $root = $mainroot);
        //dump($treelists);exit;
        dump(count($treelists));
        $childlists = tree_to_list($treelists,'_child');
        dump(count($childlists));
        //dump($childlists);exit;
        $depids = array_merge($depids,array_column($childlists,'department_id'));
        //dump(count($depids));exit;
        $depids[] = $other = 99990176;
        $depids[] = $wxdw = 99900001;
        $depids[] = $mainroot = 94001249;
        $treelists = TreeService::listToTree($main_department_lists,'department_id', $pid = 'pid', $child = '_child', $root = $mainroot);
        dump(count($treelists));
        $childlists = tree_to_list($treelists,'_child');
        dump(count($childlists));
        $depids = array_merge($depids,array_column($childlists,'department_id'));
        //dump(array_unique($depids));exit;
        $main_department_lists = Db::name("department")
            ->where(['department_id'=>['in',$depids]])
            ->select();
        //dump($main_department_lists);exit;
        $treelists = TreeService::listToTree($main_department_lists,'department_id', $pid = 'pid', $child = '_child', $root = $mainroot);
        $main_department_lists = [];
        treeToArray($treelists,$main_department_lists);
        dump($main_department_lists);exit;
        echo "new".json_encode($result);exit;
        $printNode = function($node) {
            dump($node);
        };
        // 开始遍历
        traverseTree($treelists, $printNode);
        exit;
        $result = [];
        treeToArray($treelists,$result);
        dump($result);exit;
        dump(TreeService::getChindNode($treelists,99990176));
        exit;
        dump(count(getLeafNodes($treelists)));exit;
        foreach (getLeafNodes($treelists) as $row){
            $res = GuardService::instance()->DelDepartment($row['department_id']);
            dump($res);
        }
        dump($treelists);exit;
    }
}
